package com.catmiw.csdn;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class MP3Player {
	public void testPlay(String filename)
	{
	  try {
	    File file = new File(filename);
	    AudioInputStream in= AudioSystem.getAudioInputStream(file);
	    AudioInputStream din = null;
	    AudioFormat baseFormat = in.getFormat();
	    
	    AudioFileFormat baseFileFormat =  AudioSystem.getAudioFileFormat(file);
	    System.out.println(baseFileFormat);
	    System.out.println(baseFileFormat.properties());

	    AudioFormat decodedFormat = new AudioFormat(
	    		AudioFormat.Encoding.PCM_SIGNED,
	    		baseFormat.getSampleRate(),
	    		16,
	    		baseFormat.getChannels(),
	    		baseFormat.getChannels() * 2,
	    		baseFormat.getSampleRate(),
	    		false);
	    din = AudioSystem.getAudioInputStream(decodedFormat, in);
	    // Play now. 
	    rawplay(decodedFormat, din);
	    in.close();
	  } catch (Exception e){
		  
	  }
	} 

	private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException,LineUnavailableException
	{
	  byte[] data = new byte[4096];
	  SourceDataLine line = getLine(targetFormat); 
	  if (line != null)
	  {
	    // Start
	    line.start();
	    int nBytesRead = 0;
	    int nBytesWritten = 0;
	    while (nBytesRead != -1)
	    {
	        nBytesRead = din.read(data, 0, data.length);
	        if (nBytesRead != -1) 
	        	nBytesWritten = line.write(data, 0, nBytesRead);
	    }
	    // Stop
	    line.drain();
	    line.stop();
	    line.close();
	    din.close();
	  } 
	}

	private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException {
	  SourceDataLine res = null;
	  DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
	  res = (SourceDataLine) AudioSystem.getLine(info);
	  res.open(audioFormat);
	  return res;
	} 

	public static String toHexString(byte[] b){
		if (b==null)
			return null;
		StringBuilder sb=new StringBuilder();
		for (byte x:b){
			String tt=Integer.toHexString(x&0xff);
			sb.append(tt).append("00".substring(tt.length()));
		}
		return sb.toString();
	}
	
	public static byte[] md5(String str){
		MessageDigest md;
		byte b[]=null;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			b = md.digest();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MP3Player player=new MP3Player();
		player.testPlay("e:/temp/b8a9f715dbb64fd5c56e7783c6820a61.mp3");
		player.testPlay("e:/temp/cef44b46f16ae8ecf664df4266ffdbf9.mp3");
		player.testPlay("e:/temp/6d4b62960a6aa2b1fff43a9c1d95f7b2.mp3");
		System.out.println("b8a9f715dbb64fd5c56e7783c6820a61");
		System.out.println(toHexString(md5("two")));
		System.out.println("cef44b46f16ae8ecf664df4266ffdbf9");
		System.out.println(toHexString(md5("triangle")));
		System.out.println(toHexString(md5("restaurant")));
		System.out.println(toHexString(md5("stand up")));
		System.out.println(toHexString(md5("stand_up")));
		System.out.println(toHexString(md5("stand%20up")));
		
		// http://res.iciba.com/resource/amp3/0/0/ce/f4/cef44b46f16ae8ecf664df4266ffdbf9.mp3
		// http://res-tts.iciba.com/6/d/4/6d4b62960a6aa2b1fff43a9c1d95f7b2.mp3
		//8996600c31088c6a76464ce38d965ccd.mp3
	}

}
